package com.qingcheng.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class IndexUtil {

    @Value("${elasticSearch.hostname}")
    String elasticSearchHostname;

    @Value("${elasticSearch.port}")
    String elasticSearchPort;


    /**
     * 创建索引
     * @param mapJsonString
     */
    public void createIndex(String mapJsonString){

        Map<String, Object> map = (Map<String, Object>)JSON.parseObject(mapJsonString);
        Goods goods = JSON.parseObject(JSON.toJSONString(map.get("goods")),Goods.class);
        List<Sku> skuList = goods.getSkuList();

        //1.连接rest接口
        HttpHost httpHost = new HttpHost(elasticSearchHostname,Integer.parseInt(elasticSearchPort),"http");
//        rest构建器
        RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
        //高级客户端对象
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);

        //2.封装请求对象
        BulkRequest bulkRequest = new BulkRequest();//批量请求

        for(Sku sku:skuList){
            IndexRequest indexRequest = new IndexRequest("sku", "doc",sku.getId());
            Map skuMap = new HashMap();
            skuMap.put("name",sku.getName());
            skuMap.put("price", sku.getPrice());
            skuMap.put("image", sku.getImage());
            Date createTime = sku.getCreateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = simpleDateFormat.format(createTime);
            skuMap.put("createTime",time);
            skuMap.put("spuId",sku.getSpuId());
            skuMap.put("categoryName",sku.getCategoryName());
            skuMap.put("brandName",sku.getBrandName());
            Map spec = (Map) JSON.parseObject(sku.getSpec());
            skuMap.put("spec",spec);
            skuMap.put("saleNum", sku.getSaleNum());
            skuMap.put("commentNum", sku.getCommentNum());

            indexRequest.source(skuMap);
            bulkRequest.add(indexRequest);
        }

        //3.执行且获取响应对象
        try {
            BulkResponse responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//            int status = responses.status().getStatus();
//            System.out.println("执行结果:"+status);

            //关闭接口
            restHighLevelClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除索引
     * @param mapJsonString
     */
    public void deleteIndex(String mapJsonString){

        Map<String, Object> map = (Map<String, Object>)JSON.parseObject(mapJsonString);

        String spuId = (String) map.get("spuId");

        //1.连接rest接口
        HttpHost httpHost = new HttpHost(elasticSearchHostname,Integer.parseInt(elasticSearchPort),"http");
        //rest构建器
        RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
        //高级客户端对象
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);

        //删除请求
        DeleteRequest deleteRequest = new DeleteRequest("sku");
        deleteRequest.type("doc").id(spuId);

        //3.获取响应结果
        DeleteResponse delete = null;//执行返回结果
        try {
            delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
//            int status = delete.status().getStatus();
//            System.out.println(status);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
